package cn.ekuma.android.ui.tree;

import java.util.ArrayList;
import java.util.List;

import cn.ekuma.data.ui.android.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TreeAdapter extends BaseAdapter {
    private List<TreeElement> items;
    private List<TreeElement> elements;
    private Bitmap mIconCollapse;
    private Bitmap mIconExpand;
    private Context context;
    private int selectedPosition = -1;
    private TreeCallBack callBack;
   
     public TreeAdapter(Context context) {
       this.context = context;
       mIconCollapse = BitmapFactory.decodeResource(
                 context.getResources(), R.drawable.tree_collapse);
       mIconExpand = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.tree_expand);
       items = new ArrayList<TreeElement>();
       elements= new ArrayList<TreeElement>();
     }

     
     public int getCount() {
        return items.size();
    }

   
     public Object getItem(int position) {
        return items.get(position);
   }

    
   public long getItemId(int id) {
       return id;
     }

     
    public View getView(int position, View convertView,
             ViewGroup paramViewGroup) {
         View view = LayoutInflater.from(context).inflate(
                R.layout.tree_item, null);

        view.setOnClickListener(itemOnClick);
        view.setTag(items.get(position));

         if (items.get(position).isSelected())
             view.setBackgroundColor(Color.parseColor("#FFFF33"));

        ImageView icon = (ImageView) view.findViewById(R.id.icon);
        TextView text = (TextView) view.findViewById(R.id.text);

         int level = items.get(position).getLevel();
        icon.setPadding(25 * level, 0, 0, 0);
        text.setText(items.get(position).getTitle());
         text.setPadding(0, 10, 0, 10);

        if (items.get(position).hasChildren()
                && (items.get(position).isExpanded() == false)) {
             icon.setImageBitmap(mIconCollapse);
         } else if (items.get(position).hasChildren()
                && (items.get(position).isExpanded() == true)) {
           icon.setImageBitmap(mIconExpand);
        } else if (!items.get(position).hasChildren()) {
             icon.setImageBitmap(mIconCollapse);
             icon.setVisibility(View.INVISIBLE);
         }
         return view;
     }
    
     private OnClickListener itemOnClick = new OnClickListener() {

        
         public void onClick(View view) {
            TreeElement element = (TreeElement) view.getTag();
            if (element.hasChildren()) {
                // չ������ر�
                if (element.isExpanded()) {
                     clearExpand(element);
                }
               // û��չ����չ���ڵ�
                else {
                     element.setExpanded(true);
               }
                items = new ArrayList<TreeElement>();
                createItems(elements);
                TreeAdapter.this.notifyDataSetChanged();
             } else {
            	 if(callBack!=null)
                     callBack.elementSelected(element);
            }
        }
     };

    /**
      * ����Ԫ�ص�����expand�������
     * 
      * @param element
*/
     private void clearExpand(TreeElement element) {
         element.setExpanded(false);
         if (element.hasChildren())
            for (int i = 0; i < element.childCount(); i++)
                 if (element.getChild(i).hasChildren())
                     clearExpand(element.getChild(i));
    }

     public void clear() {
 		getItems().clear();
 	}

 	public void setRootNode(List<TreeElement> buildTree) {
 		clear();
 		this.elements=buildTree;
 		createItems(buildTree);
 		notifyDataSetChanged();
 	}
     
     
     /**
      * �������е�Ԫ��
      * 
      * @param elements
*/
    public void createItems(List<TreeElement> elements) {
      for (TreeElement e : elements)
            addItem(e);
   }

    /**
    * ���������Ԫ�أ�����Ԫ�������ڵ�ʱ�ж��Ƿ��Ѿ�չ��
      * 
      * @param element
*/
     protected void addItem(TreeElement element) {
    	 System.out.println("新加入："+element.getTitle());
         items.add(element);
         if (element.isSelected())
            selectedPosition = items.size() - 1;
        if (element.hasChildren() && element.isExpanded())
             createItems(element.getChildren());
    }

	public int getSelectedPosition() {
		return selectedPosition;
	}

	public void setSelectedPosition(int selectedPosition) {
		this.selectedPosition = selectedPosition;
	}

	public List<TreeElement> getItems() {
		return items;
	}

	public void setItems(List<TreeElement> items) {
		this.items = items;
	}

	public List<TreeElement> getElements() {
		return elements;
	}

	public void setElements(List<TreeElement> elements) {
		this.elements = elements;
	}

	public void setCallBack(TreeCallBack callBack) {
		this.callBack = callBack;
	}

	public TreeCallBack getCallBack() {
		return callBack;
	} 
	
	
}